import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: undefined,
    active: '' // 活动页面
  },
  // 修改全局变量必须通过mutations中的方法
  // mutations只能采用同步方法
  mutations: {
    login (state, payload) {
      state.user = payload
    },
    logout (state) {
      state.user = undefined
    },
    active (state, value) {
      state.active = value
    }
  },
  // 异步方法用actions
  // actions 不能直接修改全局变量，需要调用commit方法来触发mutation中的方法
  actions: {
    login (context, payload) {
      context.commit('login', payload)
    },
    logout (context) {
      context.commit('logout')
    },
    active (context, value) {
      context.commit('active', value)
    }
  },
  getters: {
    account: state => {
      return state.user
    },
    active: state => {
      return state.active
    }
  },
  modules: {
  }
})
